package com.Tools.TestDb.Controller;

import com.Tools.TestDb.Entity.TestDb;
import com.Tools.TestDb.Service.TestDbService;
import com.utils.APIRespones;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TestDbController {
    @Autowired
    private TestDbService testDbService;

    @GetMapping("/api/tools/testDb/{bankType}/{count}")
    public APIRespones getTestDbByBankTypeRandom(@PathVariable String bankType, @PathVariable int count) {
        return testDbService.getTestDbByBankTypeRandom(bankType, count);
    }
    @PutMapping("/api/tools/testDb/{id}/score")
    public APIRespones updateTestDbScoreById(@PathVariable Long id, @RequestBody Map<String, Object> requestBody) {
        Object scoreObject = requestBody.get("score");
        if (scoreObject == null) {
            return APIRespones.failure("参数错误：score不能为空");
        }

        int score;
        try {
            score = Integer.parseInt(scoreObject.toString());
        } catch (NumberFormatException e) {
            return APIRespones.failure("参数错误：score应为数字类型");
        }

        TestDb testDb = testDbService.updateTestDbScoreById(id, score);
        if (testDb == null) {
            return APIRespones.failure("未找到指定id的试题");
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("testDb", testDb);

        return APIRespones.success(resultMap);
    }
}
